package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.file.service.FileStorageService;
import com.heima.file.service.FileStorageService;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmMaterialDto;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.utils.common.WmThreadLocalUtil;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.service.WmMaterialService;
import com.heima.wemedia.service.WmUserService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.Date;
import java.util.UUID;

@Service
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {
    @Autowired
    FileStorageService fileStorageService;
    @Override
    public ResponseResult uploadPicture(MultipartFile multipartFile) {
     //判断文件是否为空
        if(multipartFile.isEmpty()||multipartFile.getSize()==0){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //文件不为空

        //获取文件后缀
        String originalFilename = multipartFile.getOriginalFilename();
        int index = originalFilename.lastIndexOf(".");
        String end = originalFilename.substring(index);
        //生成文件唯一前缀
        String string = UUID.randomUUID().toString();
        String fileName = string.replace("-", "");
        //将文件保存到minio(需要文件输入流，文件名称)
        String url=null;
        try{
            InputStream inputStream = multipartFile.getInputStream();
          url= fileStorageService.uploadHtmlFile("", fileName, inputStream);
        }catch (Exception e){
            e.printStackTrace();
            log.error("文件上传失败");
        }
        WmMaterial wmMaterial =new WmMaterial();
        wmMaterial.setId(WmThreadLocalUtil.getUser().getId());
        wmMaterial.setUrl(url);
        wmMaterial.setIsCollection((short)0);
        wmMaterial.setType((short)0);
        wmMaterial.setCreatedTime(new Date());
        //将文件信息WmMaterial保存到数据库
save(wmMaterial);
        //
        return ResponseResult.okResult(wmMaterial);
    }

    @Override
    public ResponseResult findList(WmMaterialDto dto) {
        //TODO 这块利用了MybatisPlus分页查询，还没学，后面回来重写
        //检查参数
        dto.checkParam();
        //获取参数
        Integer page = dto.getPage();
        Integer size = dto.getSize();
        //1 收藏  2未收藏
        Short isCollection = dto.getIsCollection();
        //分页查询
        IPage pageModel=new Page(page,size);
        LambdaQueryWrapper<WmMaterial> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        //如果是要查询收藏的，设置条件
        if(isCollection!=null&&isCollection==1){
      //不懂这个报错      lambdaQueryWrapper.eq(WmMaterialDto::getIsCollection,dto.getIsCollection());
            lambdaQueryWrapper.eq(WmMaterial::getIsCollection,dto.getIsCollection());
        }
        //根据用户查询
        lambdaQueryWrapper.eq(WmMaterial::getId,WmThreadLocalUtil.getUser().getId());
        //根据创建时间倒序(时间晚的,大的在前面)
        lambdaQueryWrapper.orderByDesc(WmMaterial::getCreatedTime);
        page(pageModel,lambdaQueryWrapper);
        ResponseResult responseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) pageModel.getTotal());
        responseResult.setData(pageModel.getRecords());
        return responseResult;
    }
}
